<?php

namespace App\Http\Controllers\Api;


use App\Models\Bubble;
use App\Models\Prop;
use App\Models\Role;
use App\Models\Scene;
use App\Models\Category;
use App\Models\Picture;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Validate\api\ResourceValidate;
use App\Common\ConstCode;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Illuminate\Support\Arr;
class ResourceController extends BaseController
{
    public $successStatus=200;
    protected $bubble;
    protected $prop;
    protected $role;
    protected $scene;
    protected $category;
    protected $v;
    /**
     * api注册
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Bubble $bubble,Prop $prop,Role $role,Scene $scene,Category $category,ResourceValidate $v)
    {
        $this->bubble = $bubble;
        $this->prop = $prop;
        $this->role = $role;
        $this->scene = $scene;
        $this->category = $category;
        $this->v = $v;
    }


    public function lists(Request $request){
        $this->v->validate('lists');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $data['title']  = $request->input('title','');
        $data['category']  = $request->input('category','');
        $res=[];
        $member_id = Auth::guard('api')->user()->member_id;
        switch($data['type']){
            case 1:
                //场景
                $res=$this->scene->lists($data,$member_id);
                break;
            case 2:
                //角色
                $data['tag']=$request->input('tag','');
                $res=$this->role->lists($data,$member_id);
                break;
            case 3:
                //道具
                $res=$this->prop->lists($data,$member_id);
                break;
            case 4:
                //气泡
                $res=$this->bubble->lists($data,$member_id);
                break;
            case 6:
                //分镜
                $res['list']=config('cartoontag.SECOND_SCEEN');
                $res['curCount']=count($res['list']);
                break;
            default:
                break;
        }
        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            if($data['type']==5) return response()->json(['code'=>1,'data'=>[]]);
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }

    }

    public function categories(){
        $res=$this->category->lists();
        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }
    }

    public function pictures(Request $request){
        $this->v->validate('picture');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        if($data['type']==4){
            //单独处理气泡
            $res=Picture::bubble_lists($data);
        }else{
            $res=Picture::lists($data);
        }

        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }

    }

    public function rolePictures(Request $request){
        $this->v->validate('rolePicture');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $data['location']  = $request->input('location',1);
        $data['type']=Arr::get(config('cartoontag.ALBUM_TYPE'),'角色库');
        $res=Picture::lists($data);
        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }

    }

    public function picInfo(Request $request){
        $this->v->validate('picInfo');
        $data=$request->input();
        $id=$data['id'];
        $info=DB::table('cartoon_picture')->where('id',$id)->first(['pic_uri','name']);
        if($info){
            return response()->json(['code'=>1,'data'=>$info]);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }
    }

    public function tags(Request $request){
        $this->v->validate('tags');
        $data=$request->input();
        $data['type'] = $request->input('type',0);
        if($data['category']==2 && empty($data['type'])){
            return response()->json(['code'=>0,'msg'=>'传入角色类型']);
        }
        $res=$this->category->tags($data);
        return response()->json(['code'=>1,'data'=>$res]);
    }

}
